import { Injectable } from '@nestjs/common';
import { OperationLog } from './entities/log.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { ApiException } from 'src/common/filter/http-exception/api.exception';
import { ApiErrorCode } from 'src/common/enums/api-error-code.enum';
import { FindAllDto } from './dto/update-log.dto';

@Injectable()
export class LogService {
  constructor(
    @InjectRepository(OperationLog)
    private readonly operationLog: Repository<OperationLog>,
  ) {}
  // 保存操作日志
  async saveOperationLog(operationLog: OperationLog) {
    await this.operationLog.save(operationLog);
  }
  // 获取操作日志列表
  async getOperationLogList(params: FindAllDto) {
    const { current, size, username = '', title = '' } = params;
    const queryBuilder = this.operationLog.createQueryBuilder('log');
    // if (startDate && endDate) {
    //   queryBuilder.where('log.create_time BETWEEN :startDate AND :endDate', {
    //     startDate,
    //     endDate,
    //   });
    // }
    if (username) {
      queryBuilder.where('log.username = :username', { username });
    }
    if (title) {
      queryBuilder.where('log.title = :title', { title });
    }
    const [list, total] = await queryBuilder
      .skip((current - 1) * size)
      .take(size)
      .getManyAndCount();
    return { records: list, total, current, size };
  }
}
